package com.anolesoft.Planexecution.controller;

import com.anolesoft.Planexecution.dto.*;
import com.anolesoft.Planexecution.entity.MesBzzFcfhjhDo;
import com.anolesoft.Planexecution.entity.MesBzzOrderDo;
import com.anolesoft.Planexecution.mapper.MesBzzClbgMapper;
import com.anolesoft.Planexecution.mapper.StoragPlanMapper;
import com.anolesoft.Planexecution.planweb.dto.PlanWebDto;
import com.anolesoft.Planexecution.planweb.service.PlanWebService;
import com.anolesoft.Planexecution.service.StoragPlanService;
import com.anolesoft.amljmes.foreknow.bo.MesBzzQfbldmx;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.system.sys.entity.User;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.store.stk.entity.StkOutinType;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value="/storag")
public class StoragPlanController {

    @Autowired
    private StoragPlanService storagPlanService;
    @Autowired
    private PlanWebService webService;
    @Resource
    private MesBzzClbgMapper mesBzzClbgMapper;
    @Resource
    private StoragPlanMapper storagPlanMapper;


    /**
     * 查询
     * @param storagPlanDto
     * @return
     */
    @PostMapping(value = "/queryAll")
    public AjaxResult findAll(@RequestBody StoragPlanDto storagPlanDto) {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();

        StoragPlanDto storagPlanDto1 = new StoragPlanDto();
        storagPlanDto1.setFactory(loginUser.getFacCode());

        Integer pageNo = storagPlanDto.getPageNo();
        Integer pageSize = storagPlanDto.getPageSize();
        //条码号
        if(storagPlanDto.getBarCode() != null){
            String barCode = storagPlanDto.getBarCode().trim();
            storagPlanDto1.setBarCode(barCode);
            System.out.println(barCode);
        }
        //派工号
        if(storagPlanDto.getPaigonghao() != null){
            String paigonghao = storagPlanDto.getPaigonghao().trim();
            storagPlanDto1.setPaigonghao(paigonghao);
        }
        //图号
        if(storagPlanDto.getTuhao() != null){
            String tuhao = storagPlanDto.getTuhao().toUpperCase().trim();
            storagPlanDto1.setTuhao(tuhao);
        }
        //机型
        if(storagPlanDto.getJixing() != null){
            String jixing = storagPlanDto.getJixing().toUpperCase().trim();
            storagPlanDto1.setJixing(jixing);
        }
        //状态
        if(storagPlanDto.getState() != null && !storagPlanDto.getState().equals("5")) {
            String state = storagPlanDto.getState();
            storagPlanDto1.setState(state);
        }
        List<StoragPlanDto> allList = storagPlanService.queryAll(storagPlanDto1);
        int endIndex = pageNo * pageSize > allList.size() ? allList.size() : pageNo * pageSize;
        List<StoragPlanDto> list = allList.subList( (pageNo-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNo, allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

//    /**
//     * 查询当前行
//     * @param list
//     * @return
//     */
//    @PostMapping(value = "/queryRow")
//    public AjaxResult queryRow(@RequestBody List<String> list){
////        String idd = id.substring(0,id.length()-1);
//        System.out.println(list);
//        List<MesBzzFcfhjhDo> allList = storagPlanService.selectRow(list);
//        return new AjaxResult(com.anolesoft.common.core.constant.HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,allList);
//
//    }

    /**
     * 删除
     * @param list
     * @return
     */
    @PostMapping(value = "/delete")
    public AjaxResult update(@RequestBody String list){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String uid = loginUser.getEmpUid();
        String str = list.substring(0,list.length()-1);
        MesBzzOrderDo mesBzzOrderDo = new MesBzzOrderDo();
        mesBzzOrderDo.setDeleteperson(uid);
        mesBzzOrderDo.setObjuid(str);
        int a = storagPlanService.update(mesBzzOrderDo);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);

    }

    /**
     * 生成封存签
     * @param
     * @return
     */
    @PostMapping(value = "/generate")
    @Transactional
    public AjaxResult insert(@RequestBody MesBzzOrderDo mesBzzOrderDo){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
//        String str = mesBzzOrderDo.getObjuid();
        int a = storagPlanService.insert(mesBzzOrderDo);
        List<String> list = new ArrayList<>();
        list.add(mesBzzOrderDo.getObjuid());
        List<StoragPlanDto> data = storagPlanService.queryRow(list);
        StoragPlanDto storagPlanDto = new StoragPlanDto();
        storagPlanDto = data.get(0);
        storagPlanDto.setFengcunuser(loginUser.getUserUid());
        int a0 = Integer.parseInt(mesBzzOrderDo.getDbnum());
        int a1 = Integer.parseInt(storagPlanDto.getShuliang());
        int a2 = Integer.parseInt(mesBzzOrderDo.getDbnum());
        for (int i = 0; i < 100; i++) {
            if (a1 - a2 >= 0) {
                storagPlanDto.setDbnum(mesBzzOrderDo.getDbnum());
                storagPlanMapper.insertprint(storagPlanDto);
                if (a1 - a2 == 0) {
                    i = 101;
                }
                a2 += a0;
            } else {
                int a3 = a2 - a0;
                int a4 = a1 - a3;
                storagPlanDto.setDbnum(String.valueOf(a4));
                storagPlanMapper.insertprint(storagPlanDto);
                i = 101;
            }
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);

    }

    /**
     * 质量反馈-弹出
     * @param dto
     * @return
     */
    @PostMapping(value = "/feedback")
    public AjaxResult quaryProcess(@RequestBody PlanWebDto dto){
        String str = dto.getObjuid();
//        String gc = "";
//        try {
//            gc = webService.getDegsFromErp(dto);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        List<MesBzzProcessDto> a = storagPlanService.quaryProcess(str);
//        a.get(0).setDegs(gc);
        a.get(0).setDegs("15.7");
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);

    }

    /**
     * 质量反馈--确定
     * @param list
     * @return
     */
//    @PostMapping(value = "/eee")
//    public AjaxResult processDefine(@RequestBody String list){
//
//        String str = list.substring(0,list.length()-1);
//        int a = storagPlanService.processDefine(str);
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
//    }

    /**
     * 操作者检测
     * @param mesBzzProcessDto
     * @return
     */
    @Transactional
    @PostMapping(value = "/operatorTest")
    public AjaxResult updateOperator(@RequestBody MesBzzProcessDto mesBzzProcessDto){
        List<MesBzzProcessDto> all = null;
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String eid = mesBzzProcessDto.getEid();
        String fcode = loginUser.getFacCode();
        mesBzzProcessDto.setFcode(fcode);
        int b = storagPlanService.queryEmpAll(eid,fcode);
        if (b == 1){
            int a = storagPlanService.updateOperator(mesBzzProcessDto);
            if (a == 1){
                all = storagPlanService.selectAll(mesBzzProcessDto);
            }
        }
//        System.out.println(all);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,all);
    }

    /**
     * 班长检测
     * @param mesBzzProcessDto
     * @return
     */
    @Transactional
    @PostMapping(value = "/monitorobjuidTest")
    public AjaxResult updateMonitorobjuid(@RequestBody MesBzzProcessDto mesBzzProcessDto){
        List<MesBzzProcessDto> all = null;
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String eid = mesBzzProcessDto.getEid();
        String fcode = loginUser.getFacCode();
        mesBzzProcessDto.setFcode(fcode);
        int b = storagPlanService.queryEmpAll(eid,fcode);
        if (b == 1){
            int a = storagPlanService.updateMonitorobjuid(mesBzzProcessDto);
            if (a == 1){
                all = storagPlanService.selectAll(mesBzzProcessDto);
            }
        }
//        System.out.println(all);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,all);
    }

    /**
     * 检验员检测
     * @param mesBzzProcessDto
     * @return
     */
    @Transactional
    @PostMapping(value = "/jianyanyuanTest")
    public AjaxResult updateJianyanyuan(@RequestBody MesBzzProcessDto mesBzzProcessDto){
        List<MesBzzProcessDto> all = null;
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String password2 = mesBzzProcessDto.getPassword2();
        String fcode = loginUser.getFacCode();
        mesBzzProcessDto.setFcode(fcode);
        int b = storagPlanService.queryEmpAll2(password2,fcode);
        if (b != 0){
            int a = storagPlanService.updateJianyanyuan(mesBzzProcessDto);
            if (a == 1){
                all = storagPlanService.selectAll(mesBzzProcessDto);
            }
        }
        System.out.println(all);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,all);
    }


    @RequestMapping(value = "/queryRow" , method = RequestMethod.POST)
    public AjaxResult queryRow(@RequestBody List<String> objuids){
        List<StoragPlanDto> allList = storagPlanService.queryRow(objuids);
//        for (StoragPlanDto item:
//                allList) {
//
//        }
        // 去重需要测试
        allList = allList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(StoragPlanDto:: getSealsignatureno))), ArrayList::new));
        return new AjaxResult(com.anolesoft.common.core.constant.HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,allList);
    }

    /**
     * 插入产量报告单
     * @param dto
     * @return
     */
    @Transactional
    @PostMapping(value = "/insertclbg")
        public AjaxResult insertClbg(@RequestBody MesBzzClbgDto dto){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fcode = loginUser.getFacCode();
        dto.setFactoryCode(fcode);
        int a = 0;
        if (dto.getGxzj() != null) {
            dto.setGs(Double.parseDouble(dto.getDegs() )* dto.getNum());
            mesBzzClbgMapper.updateOrderState(dto);
            a =mesBzzClbgMapper.insertClbg(dto);
            int b = storagPlanMapper.kgsj(dto.getObjuid());
        }
        if (dto.getGxzj() == null) {
            int c = storagPlanMapper.wgsj(dto.getObjuid());
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }

}
